package archer.framework.utils;

import archer.framework.core.exception.BaseException;
import archer.framework.poi.command.ConstantValueResolver;
import org.jxls.command.ValueResolver;
import org.jxls.template.SimpleExporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author christ
 * @date 2016/7/12
 */
public class ExcelExporter {

    private static Logger logger = LoggerFactory.getLogger(ExcelExporter.class);

    private ValueResolver valueResolver;

    public ExcelExporter() {
        this(new ConstantValueResolver());
    }

    public ExcelExporter(ValueResolver valueResolver) {
        this.valueResolver = valueResolver;
    }

    public void simpleExport(List<String> headers, String props, Collection data, OutputStream out) {

        SimpleExporter simpleExporter = new SimpleExporter(this.valueResolver);
        try {
            simpleExporter.gridExport(headers, data, props, out);
        } catch (Exception e) {
            logger.error("EXCEL EXPORT ERROR>>", e);
            throw new BaseException(e);
        }
    }

    public void simpleExport(String headers, String props, Collection data, OutputStream out) {

        simpleExport(Arrays.asList(headers.split(",")), props, data, out);
    }
}
